//
//  VCOfSocial.m
//  XYFMApp
//
//  Created by 岁变 on 16/8/27.
//  Copyright © 2016年 岁变. All rights reserved.
//

#import "VCOfSocial.h"
#import "XYMacro.h"

@interface VCOfSocial () <TopPageBarDelegate, UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) DiscoverTopPageBar *topBar;
@property (nonatomic, strong) UICollectionView *collectionViewOfContent;

@end

@implementation VCOfSocial

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configNavigationController];
    [self configViewController];
    [self createKVO];
}


- (void)configNavigationController {
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.94 green:0.00 blue:0.00 alpha:1.00];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    titleLabel.text = @"社交";
    titleLabel.textColor = [UIColor whiteColor];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:titleLabel];
    self.navigationItem.leftBarButtonItem = leftItem;
}

- (void)configViewController {
    self.view.backgroundColor = BACKGROUNDCOLOR;
    [self createTopBar];
    [self createCollectionView];
}


- (void)createTopBar {
    _topBar = [[DiscoverTopPageBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50) withTitleArr:@[@"坦态", @"音友", @"斯信"]];
    _topBar.delegate = self;
    [self.view addSubview:_topBar];
}

- (void)createCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 50 - 64 - 49);
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionViewOfContent = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH, SCREEN_HEIGHT - 50 - 64 - 49) collectionViewLayout:layout];
    _collectionViewOfContent.dataSource = self;
    _collectionViewOfContent.backgroundColor = [UIColor whiteColor];
    _collectionViewOfContent.delegate = self;
    _collectionViewOfContent.pagingEnabled = YES;
    _collectionViewOfContent.scrollEnabled = NO;
    [self.view addSubview:_collectionViewOfContent];
    [_collectionViewOfContent registerClass:[SocialCollectionViewCellFirst class] forCellWithReuseIdentifier:@"SocialCollectionViewCellFirst"];

}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 3;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SocialCollectionViewCellFirst *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SocialCollectionViewCellFirst" forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor whiteColor];
    return cell;
}


#pragma mark ----------- KVO 键值观察 -----------------
- (void)createKVO {
    //KVO
    [_collectionViewOfContent addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    CGFloat newX = [[change objectForKey:@"new"] CGPointValue].x;
    CGFloat oldX = [[change objectForKey:@"old"] CGPointValue].x;
    
    CGFloat redX = (newX / SCREEN_WIDTH) * (SCREEN_WIDTH / 3);
    // 修改Redline位置.
    self.topBar.redLineView.transform = CGAffineTransformMakeTranslation(redX, 0);
    
    DiscoverCollectionViewCellForTopPageBar *cell1 = (DiscoverCollectionViewCellForTopPageBar *)[_topBar.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:newX / SCREEN_WIDTH inSection:0]];
    DiscoverCollectionViewCellForTopPageBar *cell2 = (DiscoverCollectionViewCellForTopPageBar *)[_topBar.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:oldX / SCREEN_WIDTH inSection:0]];
    cell2.titleLabel.textColor = [UIColor grayColor];
    cell1.titleLabel.textColor = [UIColor whiteColor];
}

- (void)dealloc {
    [_collectionViewOfContent removeObserver:self forKeyPath:@"contentOffset"];
}

- (void)didClickItemindexPath:(NSIndexPath *)indexPath PageBar:(DiscoverTopPageBar *)pageBar {
        [_collectionViewOfContent scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
